jsx3.Class.defineClass(
        "tibco.admin.monitormanage.uicomponents.chart.serviceRequests.ServiceRequests", //the full name of the class to create
        jsx3.gui.Block, //  extends
        null, // implements
        function (plugin) {
        	plugin.displayServiceRequestsChartSubject = "com.tibco.admin.monitormanage.uicomponents.chart.serviceRequests.ServiceRequests.displayServiceRequestsChartSubject";
        	plugin.numChartEntries = 60;
        
        	plugin.prototype.stackedAreaChartId = null;
        	plugin.prototype.areaChartId = null;
        	
        	plugin.prototype.isServiceInstance;
        	plugin.prototype.chartLoaded;
        
            plugin.prototype.init = function() {
                this.jsxsuper();
            }
            
            plugin.prototype.getNamespace = function() {
               return "tibco.admin";
            }
            
            plugin.prototype.setUpComponent = function(chartId, isServiceInstance) {
            	this.stackedAreaChartId = chartId + "_stackedArea";
	    	    this.areaChartId = chartId + "_area";
            
            	this.isServiceInstance = isServiceInstance;
            	this.chartLoaded = false;
                this.getDescendantOfName("totalReqHelpImg").setEvent("this.getAncestorOfName('serviceRequestsChartBlock').totalReqHelpImgClicked(objEVENT)", "jsxexecute");

                var topic = tibco.admin.monitormanage.uicomponents.page.serviceView.ServiceView.serviceInfoCacheSubject;
            	if(isServiceInstance)
            	{ topic = tibco.admin.monitormanage.uicomponents.page.serviceView.ServiceView.serviceInstanceInfoCacheSubject; }
            	tibco.admin.monitormanage.general.PageBusUtils.subscribe(topic, this.handleDisplayScreen, this);
            	tibco.admin.monitormanage.general.PageBusUtils.subscribe(plugin.displayServiceRequestsChartSubject, this.displayServiceRequestsChart, this);
            }
            
            /**
             *  This method will display the service requests chart.  This method should only be invoked if the
             *  hideServiceRequestsChart function was invoked previously to hide the chart.
             *
             *  @param stackedAreaChartIdVal the id of the chart to display.  If null is passed all instances of the
             *         service requests chart will be displayed
             */
            plugin.displayServiceRequestsChart = function(stackedAreaChartIdVal) {
            	var message = { displayChart:true };
            	if(stackedAreaChartIdVal)
            	{ message.stackedAreaChartId = stackedAreaChartIdVal; }
            	tibco.admin.monitormanage.general.PageBusUtils.publish(plugin.displayServiceRequestsChartSubject, message);
            }
            
            /**
             *  This method will hide the service requests chart.  This comes in handy considering the chart
             *  is a heavy-weight component which will cover pop up dialogs.
             *
             *  Note: if a component hides the chart it is the responsibility of that component to display the chart
             *        again when necessary.
             *
             *  @param stackedAreaChartIdVal the id of the chart to hide.  If null is passed all instances of the
             *         service requests chart will be hidden
             */
            plugin.hideServiceRequestsChart = function(stackedAreaChartIdVal) {
            	var message = { hideChart:true };
            	if(stackedAreaChartIdVal)
            	{ message.stackedAreaChartId = stackedAreaChartIdVal; }
            	tibco.admin.monitormanage.general.PageBusUtils.publish(plugin.displayServiceRequestsChartSubject, message);
            }
            
            /**
             *  Not for public consumption.
             */
            plugin.prototype.displayServiceRequestsChart = function(subject, message, serviceRequestsObj) {
            	var displayStyle = jsx3.gui.Block.DISPLAYBLOCK;
            	if(message.hideChart)
            	{ displayStyle = jsx3.gui.Block.DISPLAYNONE; }
            	if(!message.stackedAreaChartId || message.stackedAreaChartId == serviceRequestsObj.stackedAreaChartId)
            	{
	            	var topChartPane = serviceRequestsObj.getDescendantOfName("topChartPane");
    	        	var bottomChartPane = serviceRequestsObj.getDescendantOfName("bottomChartPane");
        	    	topChartPane.setDisplay(displayStyle, true);
            		bottomChartPane.setDisplay(displayStyle, true);
            	}
            }

            plugin.prototype.totalReqHelpImgClicked =function (objEVENT){
                if(this.isServiceInstance)
                this.getDescendantOfName("totalReqHelpImg").openHelp("MonitorManage.Service.Instance.TotalRequests ","tibco.admin",objEVENT);
                else
                this.getDescendantOfName("totalReqHelpImg").openHelp("MonitorManage.Service.Integrated.TotalRequests  ","tibco.admin",objEVENT);
            }
            
            plugin.prototype.handleDisplayScreen = function(subject, message, serviceRequestsObj) {
            	serviceRequestsObj.loadChart(serviceRequestsObj);
            }
            
            plugin.prototype.loadChart = function(serviceRequestsObj) {
            	var cacheId = tibco.admin.monitormanage.uicomponents.page.serviceView.ServiceView.serviceInfoCacheId;
            	if(serviceRequestsObj.isServiceInstance)
            	{ cacheId = tibco.admin.monitormanage.uicomponents.page.serviceView.ServiceView.serviceInstanceInfoCacheId; }
            	
            	var serviceInfoDoc = tibco.admin.Main.getServer().Cache.getDocument(cacheId);
            	
            	// now create any necessary empty records
            	var serviceRecord = serviceInfoDoc.getFirstChild();
            	var records = serviceRecord.getChildNodes();
            	var historyRecordCount = 0;
            	for(i = 0; i < records.size(); i++)
            	{
            		var childRecord = records.get(i);
            		var recordName = childRecord.getAttribute("recordName");
            		if(recordName == "history")
            		{ historyRecordCount++; }
            		
            		// format the time
            		var time = childRecord.getAttribute("time");
            		if(time)
            		{
            			time = tibco.admin.monitormanage.general.MonitorManageUtils.formatDateISO(time);
            			childRecord.setAttribute("time", time);
            		}
            		
            		// handle the tooltip
            		var server = tibco.admin.Main.getServer();
            		var faultsStr = server.getDynamicProperty("@MM@general@lblFaults") + ": " + childRecord.getAttribute("faults");
            		var successesStr = server.getDynamicProperty("@MM@general@lblSuccesses") + ": " + childRecord.getAttribute("successes");
            		var responseTimeStr = server.getDynamicProperty("@MM@general@lblResponseTime") + ": " + childRecord.getAttribute("responseTime") + server.getDynamicProperty("@MM@general@lblMillis");
            		var tooltip = time + "\n" + faultsStr + "\n" + successesStr + "\n" + responseTimeStr;
            		childRecord.setAttribute("tooltext",tooltip);
            		childRecord.setAttribute("tooltextResponseTime", responseTimeStr);
            	}
            	var numEmptyEntries = plugin.numChartEntries - historyRecordCount;
            	for(i = 0; i < numEmptyEntries; i++)
            	{
            		var emptyRecord = serviceRecord.createNode(jsx3.xml.Entity.TYPEELEMENT, "record");
            		emptyRecord.setAttribute("jsxid", "emptyRecordId_" + i);
            		emptyRecord.setAttribute("recordName", "history");
            		emptyRecord.setAttribute("faults", "");
            		emptyRecord.setAttribute("successes", "");
            		emptyRecord.setAttribute("responseTime", "");
            		emptyRecord.setAttribute("time", "");
            		serviceRecord.appendChild(emptyRecord);
            	}
            	
            	// transform the xml
       			var stackedAreaTransformXSL = tibco.admin.Main.loadResource("tibco/admin/monitormanage/uicomponents/chart/serviceRequests/xsl/serviceRequestsArea.xsl", serviceRequestsObj.getNamespace(), "IGNORED_ID", "xsl");
       			var stackedAreaTransformedXML = serviceInfoDoc.transformNode(stackedAreaTransformXSL);

       			var areaTransformXSL = tibco.admin.Main.loadResource("tibco/admin/monitormanage/uicomponents/chart/serviceRequests/xsl/serviceRequestsArea2D.xsl", serviceRequestsObj.getNamespace(), "IGNORED_ID2", "xsl");
           		var areaTransformedXML = serviceInfoDoc.transformNode(areaTransformXSL);
// Commenting this conditional out considering the chart update is causing a problem...will be fixed later
//            	if(!serviceRequestsObj.chartLoaded)
//            	{
           			serviceRequestsObj.chartLoaded = true;
            			
          			// Load the area chart
           			var chartId = serviceRequestsObj.stackedAreaChartId;
		       	   	var width = "100%";
           			var height = "130";
       	   			var swfURI = tibco.admin.Main.resolveURI(serviceRequestsObj.getNamespace(), "tibco/admin/monitormanage/resources/fusionCharts/swf/StackedArea2D.swf");

           			var chartPane = serviceRequestsObj.getDescendantOfName("topChartPane");
           			chartPane.setText('<OBJECT classid=\'clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\' codebase=\'http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0\' WIDTH=\'' + width + '\' HEIGHT=\'' + height + '\' id=\'' + chartId + '\'><PARAM NAME=movie VALUE=\'' + swfURI + '\'><PARAM NAME=\'FlashVars\' VALUE=\'&dataXML=' + stackedAreaTransformedXML + '&chartWidth=950&chartHeight=250\'><PARAM NAME=quality VALUE=high><PARAM NAME=bgcolor VALUE=#FFFFFF><EMBED src=\'' + swfURI + '\' FlashVars=\'&dataXML=' + stackedAreaTransformedXML + '\')\' quality=high bgcolor=#FFFFFF WIDTH=\'' + width + '\' HEIGHT=\'' + height + '\' NAME=\'' + chartId + '\' TYPE=\'application/x-shockwave-flash\' PLUGINSPAGE=\'http://www.macromedia.com/go/getflashplayer\'></EMBED></OBJECT>');
          			chartPane.repaint();
             			
		   			// Load the column chart
		   			
		   			chartId = serviceRequestsObj.areaChartId;
	   	   			width = "100%";
           			height = "60";
	   	   			swfURI = tibco.admin.Main.resolveURI(serviceRequestsObj.getNamespace(), "tibco/admin/monitormanage/resources/fusionCharts/swf/Area2D.swf");

           			chartPane = serviceRequestsObj.getDescendantOfName("bottomChartPane");
           			chartPane.setText('<OBJECT classid=\'clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\' codebase=\'http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0\' WIDTH=\'' + width + '\' HEIGHT=\'' + height + '\' id=\'' + chartId + '\'><PARAM NAME=movie VALUE=\'' + swfURI + '\'><PARAM NAME=\'FlashVars\' VALUE=\'&dataXML=' + areaTransformedXML + '&chartWidth=950&chartHeight=250\'><PARAM NAME=quality VALUE=high><PARAM NAME=bgcolor VALUE=#FFFFFF><EMBED src=\'' + swfURI + '\' FlashVars=\'&dataXML=' + areaTransformedXML + '\')\' quality=high bgcolor=#FFFFFF WIDTH=\'' + width + '\' HEIGHT=\'' + height + '\' NAME=\'' + chartId + '\' TYPE=\'application/x-shockwave-flash\' PLUGINSPAGE=\'http://www.macromedia.com/go/getflashplayer\'></EMBED></OBJECT>');
           			chartPane.repaint();
/*	            }
	            else
	            {
	            	plugin.setFusionChartData(serviceRequestsObj.stackedAreaChartId, stackedAreaTransformedXML);
	            	plugin.setFusionChartData(serviceRequestsObj.areaChartId, areaTransformedXML);
	            }
*/
            }
            
			plugin.getFusionChartObject = function(objectName) {
             	if (navigator.appName.indexOf ("Microsoft") !=-1)
             	{ return window[objectName]; } 
             	else
               	{ return document[objectName]; }
               	/*
            	if (window.document[id]) {
      				return window.document[id];
  				}
  				if (navigator.appName.indexOf("Microsoft Internet")==-1) {
    				if (document.embeds && document.embeds[id])
      					return document.embeds[id]; 
  					} else {
    				return document.getElementById(id);
  				} */
			};
			
            plugin.setFusionChartData = function(objFlash, strXML) {
            	//This function updates the data of a FusionCharts present on the page
            	//Get a reference to the movie 
            	var FCObject = plugin.getFusionChartObject(objFlash);
            	//Set the data
             	//Set dataURL to null
             	FCObject.SetVariable('_root.dataURL',"");
             	//Set the flag
             	FCObject.SetVariable('_root.isNewData',"1");
             	//Set the actual data
             	FCObject.SetVariable('_root.newData',strXML);
             	//Go to the required frame
             	FCObject.TGotoLabel('/', 'JavaScriptHandler');
          	};
		  
        });
